import requests
import re
import json


def  pageIndex():
 for x in range(1,1450):
    url ='https://mm.taobao.com/tstar/search/tstar_model.do?_input_charset=utf-8'
    headers ={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
            'Referer':'https://mm.taobao.com/search_tstar_model.htm?spm=5679.126488.640745.2.23382092wVabwy'
              }

    data ={
       'q':'',
   'viewFlag':'A',
   'sortType':'default',
    'searchStyle':'',
  'searchRegion':'city:',
    'searchFansNum':'',
   'currentPage':x,
   'pageSize':'100'}
    response = requests.post(url,headers=headers,data=data)
    print(response.status_code)
    html = response.json()
    info = html['data']['searchDOList']
    return  info


def  getPhoto(i,I):

     url ='https://mm.taobao.com/self/aiShow.htm?spm=719.7763510.1998643336.%d.4PPbtV&userId=%d'%(i,I)
     headers ={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
               }
     response = requests.get(url,headers=headers)
     html = response.text
     #print(html)
     reg = re.compile('<ul class="mm-p-menu">.*?<a href="(.*?)" >(.*?)</a>.*?</span>',re.S)
     reg = re.findall(reg,html)
     print(reg)
     url2 =reg[0][0]
     url ='https:'+url2
     print(url)


     url = 'https://mm.taobao.com/self/album/open_album_list.htm?_charset=utf-8&user_id%20='+str(I)
     print(url)
     response = requests.get(url,headers=headers)
     html =response.text
     #print(html)
     reg = re.compile('<img src="(.*?)" width=".*?" height=".*?">')
     photo =re.findall(reg,html)
     print(photo)

def  main():
    infos = pageIndex()
    print(infos)
    for i, info in enumerate(infos):
        print(i,info['userId'])
        information =[info['city'],info['realName'],info['height'],info['weight'],info['totalFavorNum'],info['userId']]
        print(information)
        getPhoto(i+1,info['userId'])



if __name__ == '__main__':
   main()
